<?php
/**
 * Created by PhpStorm.
 * User: fanzhaogui
 * Date: 2021/3/29
 * Time: 10:50
 */

namespace App\Crontab;


use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
use EasySwoole\EasySwoole\Task\TaskManager;

/**
 * Class CustomCrontab
 * @package App\Crontab
 */
class CustomCrontab extends AbstractCronTask
{
    public static function getRule(): string
    {
        # [分钟] [小时] [每月的某一天] [每年的某一月] [每周的某一天]
        return "*/1 * * * *";
    }

    public static function getTaskName(): string
    {
        return __CLASS__;
    }

    public function run(int $taskId, int $workerIndex)
    {
        // 定时任务的执行逻辑

        // 开发者可投递给task异步处理
        TaskManager::getInstance()->async(function (){
            // todo some thing
            echo __CLASS__ . ": " . __FUNCTION__ . " : " . date('Y-m-d H:i:s') . " 测试的定时任务！！", "\n";
        });
    }

    public function onException(\Throwable $throwable, int $taskId, int $workerIndex)
    {
        # 捕获run方法内抛出的异常
    }
}